﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PressureTest.Main.Project
{
    public partial class SelectTemplateForm : Form
    {
        /// <summary>DataGridView
        /// 
        /// </summary>
        private DataGridView dataGridView;

        /// <summary>模型
        /// 
        /// </summary>
        private Template obj = new Template();

        /// <summary>模板列表
        /// 
        /// </summary>
        public void LoadTemplateList()
        {
            List<Template> list = SqlSugarHelper.Inquireable<Template>(null);
            UpdateDataGridViewRow(list);
            this.HideWaitForm();
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateDataGridViewRow(List<Template> list)
        {
            if (dataGridView.Rows.Count > 1)
            {
                this.Invoke(new Action(() =>
                {
                    dataGridView.Rows.Clear();
                }));
            }

            foreach (Template item in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(dataGridViewBoxCell(item.number.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.pipe_position));
                row.Cells.Add(dataGridViewBoxCell(item.design_pressure.ToString()));

                string first = item.first_stage_pressure.ToString() + "-" + item.first_stage_minute.ToString();
                if (item.first_stage_minute <= 0)
                {
                    first = "--";
                }
                row.Cells.Add(dataGridViewBoxCell(first));

                string second = item.second_stage_pressure.ToString() + "-" + item.second_stage_minute.ToString();
                if (item.second_stage_minute <= 0)
                {
                    second = "--";
                }
                row.Cells.Add(dataGridViewBoxCell(second));

                string third = item.third_stage_pressure.ToString() + "-" + item.third_stage_minute.ToString();
                if (item.third_stage_minute <= 0)
                {
                    third = "--";
                }
                row.Cells.Add(dataGridViewBoxCell(third));

                string rigor = item.rigor_stage_pressure.ToString() + "-" + item.rigor_stage_minute.ToString();
                if (item.rigor_stage_minute <= 0)
                {
                    rigor = "--";
                }
                row.Cells.Add(dataGridViewBoxCell(rigor));

                string intensity = item.intensity_stage_pressure.ToString() + "-" + item.intensity_stage_minute.ToString();
                if (item.intensity_stage_minute <= 0)
                {
                    intensity = "--";
                }
                row.Cells.Add(dataGridViewBoxCell(intensity));

                row.Height = 40;
                this.Invoke(new Action(() =>
                {
                    dataGridView.Rows.Add(row);
                }));
            }
        }

        // <summary> 创建DataGridViewTextBoxCell
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private DataGridViewTextBoxCell dataGridViewBoxCell(string str)
        {
            DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
            cell1.Value = str;
            return cell1;
        }

        public SelectTemplateForm()
        {
            InitializeComponent();
        }

        private void SelectTemplateForm_Load(object sender, EventArgs e)
        {
            this.dataGridView = this.TemplateDataGridView;
            Task.Run(() =>
            {
                LoadTemplateList();
            });
        }

        private void TemplateDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.ColumnIndex;
            if (index < 0)
            {
                index = 0;
            }
            dataGridView.ClearSelection();
            dataGridView.Rows[e.RowIndex].Selected = true;
            dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[index];
            bool res = CheckSelectedRowExist();
            if (!res)
            {
                return;
            }
            AddProjectForm owner = (AddProjectForm)this.Owner;
            owner.template = obj;
            owner.freshTemplate();
            this.Close();
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckSelectedRowExist()
        {
            int number = SelectedRowPrimaryKey();
            obj = TemplateWithNumber(number);
            if (obj == null)
            {
                this.ShowErrorDialog2("记录不存在");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>选中记录主键值
        /// 
        /// </summary>
        /// <returns></returns>
        private int SelectedRowPrimaryKey()
        {
            int index = this.dataGridView.CurrentRow.Index;
            return this.dataGridView.Rows[index].Cells[0].Value.ToString().ToInt();
        }

        /// <summary>根据编号查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Template TemplateWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
    }
}
